package com.example.myproject.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

/**
 * Created by xi on 2017/7/30.
 */

@RestController
public class EhcacheController {

    @Autowired
    private CacheManager cacheManager;

    /**
     * ehcache.xml只会访问根目录的classpax.xml不会合并多个resource
     * @param map
     * @return
     */
    @RequestMapping("/getCache")
    public String testDruidMapper(ModelMap map) {
        Collection<String> st = cacheManager.getCacheNames();
        Cache cache = cacheManager.getCache("demo");
        cache.put("test", "1");

        // 得到缓存对象占用内存的大小
        net.sf.ehcache.Cache escahe = (net.sf.ehcache.Cache) cache.getNativeCache();
        long a = escahe.getMemoryStoreSize();
        System.out.println("a=="+a);


        String sg  = (String) cacheManager.getCache("demo").get("test").get();
        return sg;
    }
}
